package com.jh.readme.iu.fragment.news;

import com.apkfuns.logutils.LogUtils;
import com.jh.tools.api.manager.RetrofitManager;
import com.jh.tools.bean.NeteastVideoSummary;

import java.util.List;
import java.util.Map;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Func1;
import rx.functions.Func2;
import rx.schedulers.Schedulers;

/**
 * Created by niejiahuan on 2016/5/31.
 */
public class NewsModel implements NewsContract.Model {
    @Override
    public Observable<Map<String, List<NeteastVideoSummary>>> getNewsDate(final String nId, long cPage) {
        Observable<Map<String, List<NeteastVideoSummary>>> NeteastVideoSummary =
                RetrofitManager.builder("http://c.m.163.com/").getVideoListObservable(nId, cPage);
        NeteastVideoSummary
                .flatMap(
                        new Func1<Map<String, List<NeteastVideoSummary>>, Observable<NeteastVideoSummary>>() {
                            @Override
                            public Observable<NeteastVideoSummary> call(Map<String, List<NeteastVideoSummary>> map) {
                                // 通过id取到list
                                return Observable.from(map.get(nId));
                            }
                        })
                .toSortedList(new Func2<NeteastVideoSummary, NeteastVideoSummary, Integer>() {
                    @Override
                    public Integer call(NeteastVideoSummary neteastVideoSummary, NeteastVideoSummary neteastVideoSummary2) {
                        // 时间排序
                        return neteastVideoSummary.ptime.compareTo(neteastVideoSummary2.ptime);
                    }
                });
        return NeteastVideoSummary;
    }

    @Override
    public Observable getDate(Map<String, String> map) {
        return null;
    }
}
